package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Api(tags = "套餐操作")
@RequestMapping("/admin/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 分页查询
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> queryPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageResult pageResult = setmealService.queryPage(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 添加套餐
     */

    @PostMapping
    @ApiOperation("添加套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.id")
    public Result saveSetmeal(@RequestBody SetmealDTO setmealDTO) {
        setmealService.saveSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐停售与起售
     */
    @PostMapping("/status/{status}")
    @ApiOperation("套餐停售与起售")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result onOrOff(@PathVariable Integer status, Long id) {
        log.info("套餐停售与起售{}{}", status, id);
        setmealService.onOrOff(status, id);
        return Result.success();
    }

    /**
     * 修改套餐
     */
    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐{}", setmealDTO);
        setmealService.update(setmealDTO);
        return Result.success();
    }

    /**
     * 根据id查找套餐
     */
   @GetMapping("/{id}")
    @ApiOperation("根据id查找套餐")
    public Result<SetmealVO> queryById(@PathVariable Long id) {
       log.info("id参数{}",id);
      SetmealVO setmealVO = setmealService.queryById(id);
       return Result.success(setmealVO);
    }
    /**
     * 根据id批量删除
     */
    @DeleteMapping
    @ApiOperation("根据id批量删除")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deleteByIds(@RequestParam List<Long> ids){
        log.info("根据id批量删除{}",ids);
        setmealService.deleteByIds(ids);
        return Result.success();
    }
}
